﻿/*-----------------------------------------------------------------------------!
! SupplierKazImpl class                                                        !
!------------------------------------------------------------------------------!
! System   : Inventory Management System                                       !
! Client   : UK based                                                          !
! Date     : April 2011                                                        !
! Copyright: (C) KAWISOFT Inc                                                  !
! Program  : SupplierKazImpl.cs                                                !
! Function : Implementation of SupplierKaz interface                           !
!------------------------------------------------------------------------------!
! Notes:                                                                       !
! Original author: Amit Kumar Mondal                                           !
! Last changed by: Amit Kumar Mondal, May 01 2011                              !
===============================================================================*/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Model.Upadan;
using Model.Sadharon;
using Model.Poribar.ColourPanjeri;

namespace Model.Poribar.ColourPanjeri
{
    public class ColourKazImpl : ColourKaz
    {
        private Result result ;
        private ProductColor colour;
        private DBSadharon idGenerator;

        public ColourKazImpl()
        {
            colour = new ProductColor();
            result = new Result();
            idGenerator = new DBSadharon();
        }
        
        public Result create(Object colourObj)
        {
            colour = (ProductColor)colourObj;
            colour.Id = idGenerator.getUniqId(DatabaseConnection.GetConnection(),"ProductColor","PC");
            string insertString = "insert into  ProductColor values(" + "'" + colour.Id + "','"+ colour.Name +"','" + colour.isLive + "')"; ;
            try
            {
                MakeQuery(insertString).ExecuteNonQuery();
                result.IsSuccess = true;
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
            }
            finally
            {
                DatabaseConnection.sqlConnect.Close();
            }
            return result;
        }

        public Result getById(string id)
        {
            string getString = "Select * from ProductColor Where Id='" + id + "' and Live='L'";
            SqlDataReader productColorReader;

            try
            {
                productColorReader = MakeQuery(getString).ExecuteReader();
                if (productColorReader.HasRows)
                {
                    while (productColorReader.Read())
                    {
                        colour.Id = productColorReader["Id"].ToString();
                        colour.Name = productColorReader["Name"].ToString();
                    }
                    result.ResultObj = colour;
                    result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
            }
            finally {
                DatabaseConnection.sqlConnect.Close();
            }
            return result;
        }

        public Result getByName(string _name)
        {
            string getString = "Select * from ProductColor Where Name='" + _name + "' and Live='L'";
            SqlDataReader productColorReader;

            try
            {
                productColorReader = MakeQuery(getString).ExecuteReader();
                if (productColorReader.HasRows)
                {
                    while (productColorReader.Read())
                    {
                        colour.Id = productColorReader["Id"].ToString();
                        colour.Name = productColorReader["Name"].ToString();
                    }
                    result.ResultObj = colour;
                    result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
            }
            finally
            {
                DatabaseConnection.sqlConnect.Close();
            }
            return result;
        }        

        public Result All()
        {
            string findString = "select * from ProductColor where Live ='L' ";
            try
            {
                SqlDataReader productColorReader = MakeQuery(findString).ExecuteReader();
                if (productColorReader.HasRows)
                {
                    List<ProductColor> allColor = new List<ProductColor>();
                    while (productColorReader.Read())
                    {
                        colour = new ProductColor();
                        colour.Name = productColorReader["Name"].ToString();
                        colour.Id = productColorReader["Id"].ToString();
                        colour.isLive = (char)productColorReader["Live"].ToString().ToCharArray()[0];
                        allColor.Add(colour);
                    }
                    result.ResultObj = allColor;
                    result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
            }
            finally
            {
                DatabaseConnection.sqlConnect.Close();
            }
            return result;
        }
        public Result update(Object _color)
        {
            colour = (ProductColor)_color;
            string updateString = "update ProductColor set  Name='" + colour.Name + "' where Id='" + colour.Id + "'";
            try
            {
                SqlDataReader productColorReader = MakeQuery(updateString).ExecuteReader();
                if (productColorReader.HasRows)
                {
                    while (productColorReader.Read())
                    {
                        colour.Id = productColorReader["Id"].ToString();
                        colour.Name = productColorReader["Name"].ToString();
                    }
                    result.ResultObj = colour;
                    result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
            }
            finally
            {
                DatabaseConnection.sqlConnect.Close();
            } 
            return result;
        }

        public Result delete(string _id)
        {
            string updateString = "update ProductColor set  Live='D' where Id='" + _id + "'";
            try
            {
                SqlDataReader productColorReader = MakeQuery(updateString).ExecuteReader();
                if (productColorReader.HasRows)
                {
                    while (productColorReader.Read())
                    {
                        colour.Id = productColorReader["Id"].ToString();
                        colour.Name = productColorReader["Name"].ToString();
                        colour.isLive = 'D';
                    }
                    result.ResultObj = colour;
                    result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
            }
            finally
            {
                DatabaseConnection.sqlConnect.Close();
            } 
            return result;

        }

        private SqlCommand MakeQuery(string queryString)
        {
            DatabaseConnection.GetConnection().Open();
            return (new SqlCommand(queryString, DatabaseConnection.sqlConnect));
        }

    }
}
